package registnet.util.schedule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * An implementation of a Day.
 */
@SuppressWarnings("serial")
public class Day {

	public static final String MONDAY = "Monday";
	public static final String TUESDAY = "Tuesday";
	public static final String WEDNESDAY = "Wednesday";
	public static final String THURSDAY = "Thursday";
	public static final String FRIDAY = "Friday";
	public static final String SATURDAY = "Saturday";
	public static final String SUNDAY = "Sunday";
	public static final Map<String,String> MAP_DAY = new HashMap<String, String>(){{
		put("M", MONDAY);
		put("T", TUESDAY);
		put("W", WEDNESDAY);
		put("Th", THURSDAY);
		put("F", FRIDAY);
		put("S", SATURDAY);
	}};
	public static final Map<String,Integer> MAP_DAY_INT = new HashMap<String, Integer>(){{
		put("M", 0);
		put("T", 1);
		put("W", 2);
		put("Th", 3);
		put("F", 4);
		put("S", 5);
	}};
	public static final Map<Integer,String> MAP_INT_DAY = new HashMap<Integer,String>(){{
		put(0, "M");
		put(1, "T");
		put(2, "W");
		put(3, "Th");
		put(4, "F");
		put(5, "S");
	}};
	
	/**
	 * The tasks for the day.
	 */
	List<Task> tasks;
	
	/**
	 * Description of the day.
	 */
	String description;
	
	/**
	 * Default constructor which creates an "empty" Day.
	 */
	public Day(){
		tasks = new Vector<Task>();
		description = "";
	}
	
	/**
	 * Creates a day with the given description.
	 * @param description
	 */
	public Day(String description){
		tasks = new Vector<Task>();
		this.description = description;
	}
	
	/**
	 * Checks if the given task is conflicting with a task for the day.
	 * @param task
	 * @return true if the given task is conflicting with a task for the day, and false otherwise.
	 */
	public boolean isConflict(Task task){
		for(Task taskTmp : tasks){
			if(taskTmp.checkOverlap(task))
				return true;
		}	
		return false;
	}
	
	/**
	 * Adds the given task to the list of tasks for the day.
	 * @param task
	 */
	public void addTasks(Task task){
		tasks.add(task);
	}
	
	/**
	 * Parses the given <code>String</code> data.
	 * @param data
	 * @return a list of <code>String</code> description of days.
	 */
	public static List<String> tokenize(String data){
		List<String> retval = new Vector<String>();
		if(data.equals("Mon")){
			retval.add("M");
			return retval;
		}
		else if(data.equals("Tue") || data.equals("Tues")){
			retval.add("T");
			return retval;
		}
		else if(data.equals("Wed")){
			retval.add("W");
			return retval;
		}
		else if(data.equals("Thu") || data.equals("Thurs")){
			retval.add("Th");
			return retval;
		}
		else if(data.equals("Fri")){
			retval.add("F");
			return retval;
		}
		else if(data.equals("Sat")){
			retval.add("S");
			return retval;
		}else if(data.contains("-")){
			String[] tokens = data.split("-");
			int start = MAP_DAY_INT.get(tokens[0]);
			int end = MAP_DAY_INT.get(tokens[1]);
			for(int ctr=start; ctr<=end; ctr++)
				retval.add(MAP_INT_DAY.get(ctr));
			return retval;
		}else{
			if(data.contains("M")) retval.add("M");
			if(data.contains("W")) retval.add("W");
			if(data.contains("Th")) retval.add("Th");
			if(data.contains("F")) retval.add("F");
			if(data.contains("S")) retval.add("S");
			data = data.replace("Th", "X");
			if(data.contains("T")) retval.add("T");
		}
		
		return retval;
	}

	/**
	 * Returns the description of the day. 
	 * @return the description of the day
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Returns the list of tasks for the day.
	 * @return the list of tasks for the day
	 */
	public List<Task> getTasks() {
		return tasks;
	}
	
}
